//% color="#AA278D" iconWidth=50 iconHeight=40
namespace ywdcar {
  //% block="read QMC5883L data" blockType="command"
  // export function readQMC5883() {
  //   Generator.addInclude('QMC5883LCompass.h', '#include "QMC5883LCompass.h"');
  //   Generator.addObject(`QMC5883LCompass`, `QMC5883LCompass`, `compass;`);
  //   Generator.addSetup(`compass.init`, `compass.init();`);
  //   Generator.addCode(
  //     `int gyroscopeywd;\n` +
  //     `  compass.read();\n` +
  //     `  gyroscopeywd = compass.getAzimuth();\n` +
  //     `  if (gyroscopeywd < 0) {\n` +
  //     `    gyroscopeywd += 360;\n` +
  //     `  }`
  //   )
  // }

  //% block="Read gyroscopeywd data" blockType="reporter"
  // export function readGyrosc() {
  //   Generator.addCode(`gyroscopeywd`);
  // }

  //% block="Read the ultrasonic sensor distance [UNIT]"  blockType="reporter"
  //% UNIT.shadow="dropdown" UNIT.options="UNIT"
  export function readUnit(parameter: any) {
    let unit = parameter.UNIT.code;

    Generator.addInclude("DFRobot_URM10", "#include <DFRobot_URM10.h>");
    Generator.addObject(`urm10`, `DFRobot_URM10`, `urm10;`);
    Generator.addCode(`(urm10.getDistance${unit}(3, 2))`);
  }

  //% block="Start bluetooth" blockType="command"
  export function startBlueTooth() {
    Generator.addInclude(
      "void_function",
      `#define LEFT_MOTOR_PWM 10\n` +
        `#define LEFT_MOTOR_DIR1 11\n` +
        `#define LEFT_MOTOR_DIR2 12\n` +
        `#define RIGHT_MOTOR_PWM 15\n` +
        `#define RIGHT_MOTOR_DIR1 14\n` +
        `#define RIGHT_MOTOR_DIR2 13\n` +
        `#define BUZZER_PIN 18\n` +
        `#define SPEED_STEP 35\n` +
        `#define MIN_SPEED 80\n` +
        `#define MAX_SPEED 255\n` +
        `#define NUM_SPEED_LEVELS 6\n` +
        `#define SPIN_TURN_SPEED 70\n` +
        `#define DIAGONAL_SLOW_FACTOR 0.4\n` +
        `String comdata = "";\n` +
        `volatile float ywt_variable_speed_n = 150.0;\n` +
        `void beep(int frequency, int duration) {\n` +
        `  tone(BUZZER_PIN, frequency, duration);\n` +
        `  delay(duration + 50);\n` +
        `  noTone(BUZZER_PIN);\n` +
        `}\n` +
        `void indicateSpeedChange(bool isAccelerate) {\n` +
        `  if (isAccelerate) {\n` +
        `    beep(1200, 100);\n` +
        `    delay(50);\n` +
        `    beep(1200, 100);\n` +
        `  } else {\n` +
        `    beep(300, 200);\n` +
        `    delay(50);\n` +
        `    beep(300, 200);\n` +
        `  }\n` +
        `}\n` +
        `void setLeftMotor(int speed, int dir1, int dir2) {\n` +
        `    analogWrite(LEFT_MOTOR_PWM, speed);\n` +
        `    digitalWrite(LEFT_MOTOR_DIR1, dir1);\n` +
        `    digitalWrite(LEFT_MOTOR_DIR2, dir2);\n` +
        `    Serial.print("L_MOTOR: S=");\n` +
        `    Serial.print(speed);\n` +
        `    Serial.print(",D1=");\n` +
        `    Serial.print(dir1);\n` +
        `    Serial.print(",D2=");\n` +
        `    Serial.println(dir2);\n` +
        `}\n` +
        `void setRightMotor(int speed, int dir1, int dir2) {\n` +
        `    analogWrite(RIGHT_MOTOR_PWM, speed);\n` +
        `    digitalWrite(RIGHT_MOTOR_DIR1, dir1);\n` +
        `    digitalWrite(RIGHT_MOTOR_DIR2, dir2);\n` +
        `    Serial.print("R_MOTOR: S=");\n` +
        `    Serial.print(speed);\n` +
        `    Serial.print(",D1=");\n` +
        `    Serial.print(dir1);\n` +
        `    Serial.print(",D2=");\n` +
        `    Serial.println(dir2);\n` +
        `}\n` +
        `void stopMotors() {\n` +
        `    setLeftMotor(0, 0, 0);\n` +
        `    setRightMotor(0, 0, 0);\n` +
        `    Serial.println("MOTORS_STOPPED");\n` +
        `}\n` +
        `void moveForward() {\n` +
        `    Serial.println("CMD: Forward");\n` +
        `    int speed = (int)ywt_variable_speed_n;\n` +
        `    setLeftMotor(speed, 1, 0);\n` +
        `    setRightMotor(speed, 1, 0);\n` +
        `}\n` +
        `void moveBackward() {\n` +
        `    Serial.println("CMD: Backward");\n` +
        `    int speed = (int)ywt_variable_speed_n;\n` +
        `    setLeftMotor(speed, 0, 1);\n` +
        `    setRightMotor(speed, 0, 1);\n` +
        `}\n` +
        `void fixedTurnLeft() {\n` +
        `    Serial.println("CMD: Fixed Left (Spin)");\n` +
        `    int speed = SPIN_TURN_SPEED; \n` +
        `    setLeftMotor(speed, 0, 1);\n` +
        `    setRightMotor(speed, 1, 0);\n` +
        `}\n` +
        `void fixedTurnRight() {\n` +
        `    Serial.println("CMD: Fixed Right (Spin)");\n` +
        `    int speed = SPIN_TURN_SPEED; \n` +
        `    setLeftMotor(speed, 1, 0);\n` +
        `    setRightMotor(speed, 0, 1);\n` +
        `}\n` +
        `void moveForwardLeft() {\n` +
        `    Serial.println("CMD: Forward Left");\n` +
        `    int fastSpeed = (int)ywt_variable_speed_n;\n` +
        `    int slowSpeed = (int)(fastSpeed * DIAGONAL_SLOW_FACTOR);\n` +
        `    slowSpeed = max(MIN_SPEED, slowSpeed);\n` +
        `    setLeftMotor(slowSpeed, 1, 0);\n` +
        `    setRightMotor(fastSpeed, 1, 0);\n` +
        `}\n` +
        `void moveForwardRight() {\n` +
        `    Serial.println("CMD: Forward Right");\n` +
        `    int fastSpeed = (int)ywt_variable_speed_n;\n` +
        `    int slowSpeed = (int)(fastSpeed * DIAGONAL_SLOW_FACTOR);\n` +
        `    slowSpeed = max(MIN_SPEED, slowSpeed);\n` +
        `    setLeftMotor(fastSpeed, 1, 0);\n` +
        `    setRightMotor(slowSpeed, 1, 0);\n` +
        `}\n` +
        `void moveBackwardLeft() {\n` +
        `    Serial.println("CMD: Backward Left");\n` +
        `    int fastSpeed = (int)ywt_variable_speed_n;\n` +
        `    int slowSpeed = (int)(fastSpeed * DIAGONAL_SLOW_FACTOR);\n` +
        `    slowSpeed = max(MIN_SPEED, slowSpeed);\n` +
        `    setLeftMotor(slowSpeed, 0, 1);\n` +
        `    setRightMotor(fastSpeed, 0, 1);\n` +
        `}\n` +
        `void moveBackwardRight() {\n` +
        `    Serial.println("CMD: Backward Right");\n` +
        `    int fastSpeed = (int)ywt_variable_speed_n;\n` +
        `    int slowSpeed = (int)(fastSpeed * DIAGONAL_SLOW_FACTOR);\n` +
        `    slowSpeed = max(MIN_SPEED, slowSpeed);\n` +
        `    setLeftMotor(fastSpeed, 0, 1);\n` +
        `    setRightMotor(slowSpeed, 0, 1);\n` +
        `}\n`
    );
    Generator.addSetup(
      "bluetooth_setup_code",
      `Serial.begin(9600);\n` +
        `  delay(2000);\n` +
        `  Serial.println("--- Robot Car Control Started ---");\n` +
        `  Serial.print("Initial Speed Variable: ");\n` +
        `  Serial.println(ywt_variable_speed_n);\n` +
        `  Serial.print("Speed Step: ");\n` +
        `  Serial.println(SPEED_STEP);\n` +
        `  Serial.print("Min Speed: ");\n` +
        `  Serial.println(MIN_SPEED);\n` +
        `  Serial.print("Max Speed: ");\n` +
        `  Serial.println(MAX_SPEED);\n` +
        `  Serial.print("Number of Speed Levels: ");\n` +
        `  Serial.println(NUM_SPEED_LEVELS);\n` +
        `  Serial.print("Fixed Spin Turn Speed: ");\n` +
        `  Serial.println(SPIN_TURN_SPEED);\n` +
        `  Serial.print("Diagonal Slow Factor: ");\n` +
        `  Serial.println(DIAGONAL_SLOW_FACTOR);\n` +
        `  Serial.println("Debug output via USB Serial (9600 baud)");\n` +
        `  pinMode(LEFT_MOTOR_PWM, OUTPUT);\n` +
        `  pinMode(LEFT_MOTOR_DIR1, OUTPUT);\n` +
        `  pinMode(LEFT_MOTOR_DIR2, OUTPUT);\n` +
        `  pinMode(RIGHT_MOTOR_PWM, OUTPUT);\n` +
        `  pinMode(RIGHT_MOTOR_DIR1, OUTPUT);\n` +
        `  pinMode(RIGHT_MOTOR_DIR2, OUTPUT);\n` +
        `  Serial.println("Motor pins initialized.");\n` +
        `  pinMode(BUZZER_PIN, OUTPUT);\n` +
        `  Serial.println("Buzzer pin initialized.");\n` +
        `  Serial2.begin(9600); \n` +
        `  Serial.println("Bluetooth Serial2 started (9600 baud)");\n` +
        `  stopMotors();\n` +
        `  Serial.println("--- Setup Complete ---");`
    );
    Generator.addCode(
      `while (Serial2.available() > 0) {\n` +
        `    char receivedChar = Serial2.read();\n` +
        `    comdata += receivedChar;\n` +
        `    delay(2);\n` +
        `  }\n` +
        `  if (comdata.length() > 0) {\n` +
        `    Serial.print("Received Command: '");\n` +
        `    Serial.print(comdata);\n` +
        `    Serial.println("'");\n` +
        `    comdata.trim();\n` +
        `    if (comdata == "BLE_Forward") {\n` +
        `      moveForward();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Backward") {\n` +
        `      moveBackward();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_C") {\n` +
        `      fixedTurnLeft();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_D") {\n` +
        `      fixedTurnRight();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Forward_Left") {\n` +
        `      moveForwardLeft();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Forward_Right") {\n` +
        `      moveForwardRight();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Backward_Left") {\n` +
        `      moveBackwardLeft();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Backward_Right") {\n` +
        `      moveBackwardRight();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Stop") {\n` +
        `      stopMotors();\n` +
        `    }\n` +
        `    else if ((comdata == "BLE_Accelerate") && (ywt_variable_speed_n <= (MAX_SPEED - SPEED_STEP))) {\n` +
        `      Serial.print("CMD: Accelerate. Old Speed Var: ");\n` +
        `      Serial.println(ywt_variable_speed_n);\n` +
        `      ywt_variable_speed_n += SPEED_STEP;\n` +
        `      Serial.print("CMD: Accelerate. New Speed Var: ");\n` +
        `      Serial.println(ywt_variable_speed_n);\n` +
        `      indicateSpeedChange(true);\n` +
        `    }\n` +
        `    else if ((comdata == "BLE_Moderate") && (ywt_variable_speed_n >= (MIN_SPEED + SPEED_STEP))) {\n` +
        `      Serial.print("CMD: Moderate. Old Speed Var: ");\n` +
        `      Serial.println(ywt_variable_speed_n);\n` +
        `      ywt_variable_speed_n -= SPEED_STEP;\n` +
        `      Serial.print("CMD: Deccelerate. New Speed Var: ");\n` +
        `      Serial.println(ywt_variable_speed_n);\n` +
        `      indicateSpeedChange(false);\n` +
        `    }\n` +
        `    else {\n` +
        `      Serial.println("Unknown command");\n` +
        `      Serial.print(comdata);\n` +
        `      Serial.println("'");\n` +
        `    }\n` +
        `    comdata = "";\n` +
        `  }\n` +
        `  delay(10);`
    );
  }

  //% block="Read bluetooth is [BL]" blockType="boolean"
  //% BL.shadow="dropdown" BL.options="BL_OPTIONS_CUSTOM"
  export function readBlueTooth(param: any) {
    let bl = param.BL.code;
    Generator.addCode([
      `(comdata.length() > 0) && (comdata==String("${bl}"))`,
      Generator.ORDER_UNARY_POSTFIX,
    ]);
  }

  //% block="Start bluetooth plus" blockType="command"
  export function startBlueToothPlus() {
    Generator.addInclude(
      "void_function",
      `#define LEFT_MOTOR_PWM 10\n` +
        `#define LEFT_MOTOR_DIR1 11\n` +
        `#define LEFT_MOTOR_DIR2 12\n` +
        `#define RIGHT_MOTOR_PWM 15\n` +
        `#define RIGHT_MOTOR_DIR1 14\n` +
        `#define RIGHT_MOTOR_DIR2 13\n` +
        `#define BUZZER_PIN 18\n` +
        `#define SPEED_STEP 35\n` +
        `#define MIN_SPEED 80\n` +
        `#define MAX_SPEED 255\n` +
        `#define NUM_SPEED_LEVELS 6\n` +
        `#define SPIN_TURN_SPEED 70\n` +
        `#define DIAGONAL_SLOW_FACTOR 0.4\n` +
        `String comdata = "";\n` +
        `volatile float ywt_variable_speed_n = 150.0;\n` +
        `void beep(int frequency, int duration) {\n` +
        `  tone(BUZZER_PIN, frequency, duration);\n` +
        `  delay(duration + 50);\n` +
        `  noTone(BUZZER_PIN);\n` +
        `}\n` +
        `void indicateSpeedChange(bool isAccelerate) {\n` +
        `  if (isAccelerate) {\n` +
        `    beep(1200, 100);\n` +
        `    delay(50);\n` +
        `    beep(1200, 100);\n` +
        `  } else {\n` +
        `    beep(300, 200);\n` +
        `    delay(50);\n` +
        `    beep(300, 200);\n` +
        `  }\n` +
        `}\n` +
        `void setLeftMotor(int speed, int dir1, int dir2) {\n` +
        `    analogWrite(LEFT_MOTOR_PWM, speed);\n` +
        `    digitalWrite(LEFT_MOTOR_DIR1, dir1);\n` +
        `    digitalWrite(LEFT_MOTOR_DIR2, dir2);\n` +
        `    Serial.print("L_MOTOR: S=");\n` +
        `    Serial.print(speed);\n` +
        `    Serial.print(",D1=");\n` +
        `    Serial.print(dir1);\n` +
        `    Serial.print(",D2=");\n` +
        `    Serial.println(dir2);\n` +
        `}\n` +
        `void setRightMotor(int speed, int dir1, int dir2) {\n` +
        `    analogWrite(RIGHT_MOTOR_PWM, speed);\n` +
        `    digitalWrite(RIGHT_MOTOR_DIR1, dir1);\n` +
        `    digitalWrite(RIGHT_MOTOR_DIR2, dir2);\n` +
        `    Serial.print("R_MOTOR: S=");\n` +
        `    Serial.print(speed);\n` +
        `    Serial.print(",D1=");\n` +
        `    Serial.print(dir1);\n` +
        `    Serial.print(",D2=");\n` +
        `    Serial.println(dir2);\n` +
        `}\n` +
        `void stopMotors() {\n` +
        `    setLeftMotor(0, 0, 0);\n` +
        `    setRightMotor(0, 0, 0);\n` +
        `    Serial.println("MOTORS_STOPPED");\n` +
        `}\n` +
        `void moveForward() {\n` +
        `    Serial.println("CMD: Forward");\n` +
        `    int speed = (int)ywt_variable_speed_n;\n` +
        `    setLeftMotor(speed, 1, 0);\n` +
        `    setRightMotor(speed, 1, 0);\n` +
        `}\n` +
        `void moveBackward() {\n` +
        `    Serial.println("CMD: Backward");\n` +
        `    int speed = (int)ywt_variable_speed_n;\n` +
        `    setLeftMotor(speed, 0, 1);\n` +
        `    setRightMotor(speed, 0, 1);\n` +
        `}\n` +
        `void fixedTurnLeft() {\n` +
        `    Serial.println("CMD: Fixed Left (Spin)");\n` +
        `    int speed = SPIN_TURN_SPEED; \n` +
        `    setLeftMotor(speed, 0, 1);\n` +
        `    setRightMotor(speed, 1, 0);\n` +
        `}\n` +
        `void fixedTurnRight() {\n` +
        `    Serial.println("CMD: Fixed Right (Spin)");\n` +
        `    int speed = SPIN_TURN_SPEED; \n` +
        `    setLeftMotor(speed, 1, 0);\n` +
        `    setRightMotor(speed, 0, 1);\n` +
        `}\n` +
        `void moveForwardLeft() {\n` +
        `    Serial.println("CMD: Forward Left");\n` +
        `    int fastSpeed = (int)ywt_variable_speed_n;\n` +
        `    int slowSpeed = (int)(fastSpeed * DIAGONAL_SLOW_FACTOR);\n` +
        `    slowSpeed = max(MIN_SPEED, slowSpeed);\n` +
        `    setLeftMotor(slowSpeed, 1, 0);\n` +
        `    setRightMotor(fastSpeed, 1, 0);\n` +
        `}\n` +
        `void moveForwardRight() {\n` +
        `    Serial.println("CMD: Forward Right");\n` +
        `    int fastSpeed = (int)ywt_variable_speed_n;\n` +
        `    int slowSpeed = (int)(fastSpeed * DIAGONAL_SLOW_FACTOR);\n` +
        `    slowSpeed = max(MIN_SPEED, slowSpeed);\n` +
        `    setLeftMotor(fastSpeed, 1, 0);\n` +
        `    setRightMotor(slowSpeed, 1, 0);\n` +
        `}\n` +
        `void moveBackwardLeft() {\n` +
        `    Serial.println("CMD: Backward Left");\n` +
        `    int fastSpeed = (int)ywt_variable_speed_n;\n` +
        `    int slowSpeed = (int)(fastSpeed * DIAGONAL_SLOW_FACTOR);\n` +
        `    slowSpeed = max(MIN_SPEED, slowSpeed);\n` +
        `    setLeftMotor(slowSpeed, 0, 1);\n` +
        `    setRightMotor(fastSpeed, 0, 1);\n` +
        `}\n` +
        `void moveBackwardRight() {\n` +
        `    Serial.println("CMD: Backward Right");\n` +
        `    int fastSpeed = (int)ywt_variable_speed_n;\n` +
        `    int slowSpeed = (int)(fastSpeed * DIAGONAL_SLOW_FACTOR);\n` +
        `    slowSpeed = max(MIN_SPEED, slowSpeed);\n` +
        `    setLeftMotor(fastSpeed, 0, 1);\n` +
        `    setRightMotor(slowSpeed, 0, 1);\n` +
        `}\n`
    );
    Generator.addSetup(
      "bluetooth_setup_code",
      `Serial.begin(9600);\n` +
        `  delay(2000);\n` +
        `  Serial.println("--- Robot Car Control Started ---");\n` +
        `  Serial.print("Initial Speed Variable: ");\n` +
        `  Serial.println(ywt_variable_speed_n);\n` +
        `  Serial.print("Speed Step: ");\n` +
        `  Serial.println(SPEED_STEP);\n` +
        `  Serial.print("Min Speed: ");\n` +
        `  Serial.println(MIN_SPEED);\n` +
        `  Serial.print("Max Speed: ");\n` +
        `  Serial.println(MAX_SPEED);\n` +
        `  Serial.print("Number of Speed Levels: ");\n` +
        `  Serial.println(NUM_SPEED_LEVELS);\n` +
        `  Serial.print("Fixed Spin Turn Speed: ");\n` +
        `  Serial.println(SPIN_TURN_SPEED);\n` +
        `  Serial.print("Diagonal Slow Factor: ");\n` +
        `  Serial.println(DIAGONAL_SLOW_FACTOR);\n` +
        `  Serial.println("Debug output via USB Serial (9600 baud)");\n` +
        `  pinMode(LEFT_MOTOR_PWM, OUTPUT);\n` +
        `  pinMode(LEFT_MOTOR_DIR1, OUTPUT);\n` +
        `  pinMode(LEFT_MOTOR_DIR2, OUTPUT);\n` +
        `  pinMode(RIGHT_MOTOR_PWM, OUTPUT);\n` +
        `  pinMode(RIGHT_MOTOR_DIR1, OUTPUT);\n` +
        `  pinMode(RIGHT_MOTOR_DIR2, OUTPUT);\n` +
        `  Serial.println("Motor pins initialized.");\n` +
        `  pinMode(BUZZER_PIN, OUTPUT);\n` +
        `  Serial.println("Buzzer pin initialized.");\n` +
        `  Serial2.begin(9600); \n` +
        `  Serial.println("Bluetooth Serial2 started (9600 baud)");\n` +
        `  stopMotors();\n` +
        `  Serial.println("--- Setup Complete ---");`
    );
    Generator.addCode(
      `while (Serial2.available() > 0) {\n` +
        `    char receivedChar = Serial2.read();\n` +
        `    comdata += receivedChar;\n` +
        `    delay(2);\n` +
        `  }\n` +
        `  if (comdata.length() > 0) {\n` +
        `    Serial.print("Received Command: '");\n` +
        `    Serial.print(comdata);\n` +
        `    Serial.println("'");\n` +
        `    comdata.trim();\n` +
        `    if (comdata == "BLE_Forward") {\n` +
        `      moveForward();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Backward") {\n` +
        `      moveBackward();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_C") {\n` +
        `      fixedTurnLeft();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_D") {\n` +
        `      fixedTurnRight();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Forward_Left") {\n` +
        `      moveForwardLeft();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Forward_Right") {\n` +
        `      moveForwardRight();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Backward_Left") {\n` +
        `      moveBackwardLeft();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Backward_Right") {\n` +
        `      moveBackwardRight();\n` +
        `    }\n` +
        `    else if (comdata == "BLE_Stop") {\n` +
        `      stopMotors();\n` +
        `    }\n` +
        `    else if ((comdata == "BLE_Accelerate") && (ywt_variable_speed_n <= (MAX_SPEED - SPEED_STEP))) {\n` +
        `      Serial.print("CMD: Accelerate. Old Speed Var: ");\n` +
        `      Serial.println(ywt_variable_speed_n);\n` +
        `      ywt_variable_speed_n += SPEED_STEP;\n` +
        `      Serial.print("CMD: Accelerate. New Speed Var: ");\n` +
        `      Serial.println(ywt_variable_speed_n);\n` +
        `      indicateSpeedChange(true);\n` +
        `    }\n` +
        `    else if ((comdata == "BLE_Moderate") && (ywt_variable_speed_n >= (MIN_SPEED + SPEED_STEP))) {\n` +
        `      Serial.print("CMD: Moderate. Old Speed Var: ");\n` +
        `      Serial.println(ywt_variable_speed_n);\n` +
        `      ywt_variable_speed_n -= SPEED_STEP;\n` +
        `      Serial.print("CMD: Deccelerate. New Speed Var: ");\n` +
        `      Serial.println(ywt_variable_speed_n);\n` +
        `      indicateSpeedChange(false);\n` +
        `    }\n` +
        `    else {\n` +
        `      Serial.println("Unknown command");\n` +
        `      Serial.print(comdata);\n` +
        `      Serial.println("'");\n` +
        `    }\n` +
        // `    comdata = "";\n` +
        `  }\n` +
        `  delay(10);`
    );
  }

  //% block="clear comdata" blockType="command"
  export function clearComdata() {
    Generator.addCode(`comdata = "";`);
  }

  //% block="Start bluetooth" blockType="command"
  // export function startBluetooth() {
  //   Generator.addInclude('string_comdata', `String comdata = "";`)
  //   Generator.addInclude('volatile_float_sudu', `volatile float ywt_variable_speed_n;`)
  //   // 函数声明
  //   Generator.addInclude('DF_HouZuo_YWT_declare', `void DF_HouZuo_YWT();`)
  //   Generator.addInclude('DF_HouYou_YWT_declare', `void DF_HouYou_YWT();`)
  //   Generator.addInclude('DF_QianZuo_YWT_declare', `void DF_QianZuo_YWT();`)
  //   Generator.addInclude('DF_QianYou_YWT_declare', `void DF_QianYou_YWT();`)
  //   Generator.addInclude('DF_ZuoZhuan_YWT_declare', `void DF_ZuoZhuan_YWT();`)
  //   Generator.addInclude('DF_YouZhuan_YWT_declare', `void DF_YouZhuan_YWT();`)
  //   Generator.addInclude('DF_HouTui_YWT_declare', `void DF_HouTui_YWT();`)
  //   Generator.addInclude('DF_QianJin_YWT_declare', `void DF_QianJin_YWT();`)
  //   // 自定义函数
  //   Generator.addInclude('DF_HouZuo_YWT_function', `void DF_HouZuo_YWT() {\n` +
  //     `  analogWrite(10, ywt_variable_speed_n);\n` +
  //     `  digitalWrite(11, 0);\n` +
  //     `  digitalWrite(12, 1);\n` +
  //     `  analogWrite(15, (ywt_variable_speed_n + 40));\n` +
  //     `  digitalWrite(14, 1);\n` +
  //     `  digitalWrite(13, 0);\n` +
  //     `}`
  //   )
  //   Generator.addInclude('DF_HouYou_YWT_function', `void DF_HouYou_YWT() {\n` +
  //     `  analogWrite(10, (ywt_variable_speed_n + 30));\n` +
  //     `  digitalWrite(11, 0);\n` +
  //     `  digitalWrite(12, 1);\n` +
  //     `  analogWrite(15, ywt_variable_speed_n);\n` +
  //     `  digitalWrite(14, 1);\n` +
  //     `  digitalWrite(13, 0);\n` +
  //     `}`
  //   )
  //   Generator.addInclude('DF_QianZuo_YWT_function', `void DF_QianZuo_YWT() {\n` +
  //     `  analogWrite(10, ywt_variable_speed_n);\n` +
  //     `  digitalWrite(11, 1);\n` +
  //     `  digitalWrite(12, 0);\n` +
  //     `  analogWrite(15, (ywt_variable_speed_n + 40));\n` +
  //     `  digitalWrite(14, 0);\n` +
  //     `  digitalWrite(13, 1);\n` +
  //     `}`
  //   )
  //   Generator.addInclude('DF_QianYou_YWT_function', `void DF_QianYou_YWT() {\n` +
  //     `  analogWrite(10, (ywt_variable_speed_n + 30));\n` +
  //     `  digitalWrite(11, 1);\n` +
  //     `  digitalWrite(12, 0);\n` +
  //     `  analogWrite(15, ywt_variable_speed_n);\n` +
  //     `  digitalWrite(14, 0);\n` +
  //     `  digitalWrite(13, 1);\n` +
  //     `}`
  //   )
  //   Generator.addInclude('DF_ZuoZhuan_YWT_function', `void DF_ZuoZhuan_YWT() {\n` +
  //     `  analogWrite(10, (ywt_variable_speed_n - 40));\n` +
  //     `  digitalWrite(11, 0);\n` +
  //     `  digitalWrite(12, 1);\n` +
  //     `  analogWrite(15, (ywt_variable_speed_n - 40));\n` +
  //     `  digitalWrite(14, 0);\n` +
  //     `  digitalWrite(13, 1);\n` +
  //     `}`
  //   )
  //   Generator.addInclude('DF_YouZhuan_YWT_function', `void DF_YouZhuan_YWT() {\n` +
  //     `  analogWrite(10, (ywt_variable_speed_n - 40));\n` +
  //     `  digitalWrite(11, 1);\n` +
  //     `  digitalWrite(12, 0);\n` +
  //     `  analogWrite(15, (ywt_variable_speed_n - 40));\n` +
  //     `  digitalWrite(14, 1);\n` +
  //     `  digitalWrite(13, 0);\n` +
  //     `}`
  //   )
  //   Generator.addInclude('DF_HouTui_YWT_function', `void DF_HouTui_YWT() {\n` +
  //     `  analogWrite(10, ywt_variable_speed_n);\n` +
  //     `  digitalWrite(11, 0);\n` +
  //     `  digitalWrite(12, 1);\n` +
  //     `  analogWrite(15, ywt_variable_speed_n);\n` +
  //     `  digitalWrite(14, 1);\n` +
  //     `  digitalWrite(13, 0);\n` +
  //     `}`
  //   )
  //   Generator.addInclude('DF_QianJin_YWT_function', `void DF_QianJin_YWT() {\n` +
  //     `  analogWrite(10, ywt_variable_speed_n);\n` +
  //     `  digitalWrite(11, 1);\n` +
  //     `  digitalWrite(12, 0);\n` +
  //     `  analogWrite(15, ywt_variable_speed_n);\n` +
  //     `  digitalWrite(14, 0);\n` +
  //     `  digitalWrite(13, 1);\n` +
  //     `}`
  //   )
  //   // 初始化
  //   Generator.addSetup(`Serial2.begin`, `Serial2.begin(9600);`);
  //   Generator.addSetup(`pinMode_11_output`, `pinMode(11, OUTPUT);`);
  //   Generator.addSetup(`pinMode_12_output`, `pinMode(12, OUTPUT);`);
  //   Generator.addSetup(`pinMode_13_output`, `pinMode(13, OUTPUT);`);
  //   Generator.addSetup(`pinMode_14_output`, `pinMode(14, OUTPUT);`);
  //   Generator.addSetup(`speed_setup`, `ywt_variable_speed_n = 100;`);
  //   // 追加代码
  //   Generator.addCode([
  //     `while (Serial2.available() > 0) {\n` +
  //     `    comdata += char(Serial2.read());\n` +
  //     `    delay(2);\n` +
  //     `  }`,
  //     Generator.ORDER_UNARY_POSTFIX
  //   ])
  //   Generator.addCode(`if (comdata.length() > 0){\n` +
  //     `    if (comdata==String("BLE_Forward")) {\n` +
  //     `      DF_QianJin_YWT();\n` +
  //     `    }\n` +
  //     `    if (comdata==String("BLE_Left")) {\n` +
  //     `      DF_ZuoZhuan_YWT();\n` +
  //     `    }\n` +
  //     `    if (comdata==String("BLE_Right")) {\n` +
  //     `      DF_YouZhuan_YWT();\n` +
  //     `    }\n` +
  //     `    if (comdata==String("BLE_Backward")) {\n` +
  //     `      DF_HouTui_YWT();\n` +
  //     `    }\n` +
  //     `    if (comdata==String("BLE_Forward_Left")) {\n` +
  //     `      DF_QianZuo_YWT();\n` +
  //     `    }\n` +
  //     `    if (comdata==String("BLE_Forward_Right")) {\n` +
  //     `      DF_QianYou_YWT();\n` +
  //     `    }\n` +
  //     `    if (comdata==String("BLE_Backward_Right")) {\n` +
  //     `      DF_HouZuo_YWT();\n` +
  //     `    }\n` +
  //     `    if (comdata==String("BLE_Backward_Left")) {\n` +
  //     `      DF_HouYou_YWT();\n` +
  //     `    }\n` +
  //     `    if ((comdata==String("BLE_Accelerate")) && (ywt_variable_speed_n < 161)) {\n` +
  //     `      ywt_variable_speed_n += 30;\n` +
  //     `      analogWrite(18, 127);\n` +
  //     `      delay(200);\n` +
  //     `      analogWrite(18, 0);\n` +
  //     `    }\n` +
  //     `    if ((comdata==String("BLE_Moderate")) && (ywt_variable_speed_n > 69)) {\n` +
  //     `      ywt_variable_speed_n -= 30;\n` +
  //     `      analogWrite(18, 231);\n` +
  //     `      delay(300);\n` +
  //     `      analogWrite(18, 0);\n` +
  //     `    }\n` +
  //     `    if (comdata==String("BLE_Stop")) {\n` +
  //     `      digitalWrite(11, 1);\n` +
  //     `      digitalWrite(12, 1);\n` +
  //     `      digitalWrite(13, 1);\n` +
  //     `      digitalWrite(14, 1);\n` +
  //     `    }\n` +
  //     `    comdata = "";\n` +
  //     `  }`
  //   )
  // }

  // //% block="BlueTooth initialization" blockType="command"
  // export function initBlueTooth() {
  //   Generator.addInclude('string_comdata', `String comdata = "";`)
  //   Generator.addSetup(`Serial2.begin`, `Serial2.begin(9600);`);
  //   Generator.addCode([
  //     `while (Serial2.available() > 0) {\n` +
  //     `    comdata += char(Serial2.read());\n` +
  //     `    delay(2);\n` +
  //     `  }`,
  //     Generator.ORDER_UNARY_POSTFIX
  //   ])
  // }

  // //% block="Read bluetooth is [BL]" blockType="boolean"
  // //% BL.shadow="dropdown" BL.options="BL_OPTIONS"
  // export function readBlueTooth(param: any) {
  //   let bl = param.BL.code
  //   Generator.addCode([`(comdata.length() > 0) && (comdata==String("${bl}"))`, Generator.ORDER_UNARY_POSTFIX])
  // }

  // //% block="clear comdata" blockType="command"
  // export function clearComdata() {
  //   Generator.addCode(`if (comdata.length() > 0) {\n` +
  //     `    comdata = "";\n` +
  //     `  }`);
  // }

  //% block="Read pin GP16 infrared received value"  blockType="reporter"
  export function readGp16() {
    Generator.addInclude("DFRobot_IRremote_1", '#include "remote.h"');
    Generator.addObject(
      `remoteReceive_16`,
      `IRremote_Receive`,
      `remoteReceive_16;`
    );
    Generator.addSetup(`remoteReceive_16.begin`, `remoteReceive_16.begin(16);`);
    Generator.addCode(`(remoteReceive_16.getIrCommand())`);
  }

  //% block="Read pin GP16 infrared received value [KEY] is pressed" blockType="boolean"
  //% KEY.shadow="dropdown" KEY.options="KEY_OPTIONS"
  export function readKey(parameter: any) {
    let key = parameter.KEY.code;
    Generator.addInclude("DFRobot_IRremote_1", '#include "remote.h"');
    Generator.addObject(
      `remoteReceive_16`,
      `IRremote_Receive`,
      `remoteReceive_16;`
    );
    Generator.addSetup(`remoteReceive_16.begin`, `remoteReceive_16.begin(16);`);
    Generator.addCode([
      `remoteReceive_16.getIrCommand() == String("${key}")`,
      Generator.ORDER_UNARY_POSTFIX,
    ]);
  }

  //% block="the infrared remote [KEY] is" blockType="reporter"
  //% PIN.shadow="dropdown" PIN.options="PIN_DigitalWrite"
  //% KEY.shadow="dropdown" KEY.options="KEY_OPTIONS"
  export function readKey1(parameter: any) {
    // let pin = parameter.PIN.code;
    let key = parameter.KEY.code;
    // Generator.addInclude('DFRobot_IRremote', '#include <DFRobot_IRremote.h>');
    // Generator.addObject(`remoteReceive_${pin}`, `IRremote_Receive`, `remoteReceive_${pin};`);
    // Generator.addSetup(`remoteReceive_${pin}.begin`, `remoteReceive_${pin}.begin(${pin});`);
    // Generator.addCode([`remoteReceive_${pin}.getIrCommand()==String("${key}")`, Generator.ORDER_UNARY_POSTFIX]);
    Generator.addCode(`String("${key}")`);
  }

  //% block="LED [STATE]" blockType="command"
  //% STATE.shadow="dropdown" STATE.options="STATE_OPTIONS"
  // export function ledState(param: any) {
  //   let status = param.STATE.code;
  //   Generator.addSetup(`pinMode17Output`, `pinMode(17, OUTPUT);`);
  //   Generator.addCode(`digitalWrite(17, ${status});`);
  // }

  // //% block="infrared value is [KEY]"  blockType="reporter"
  // //% KEY.shadow="dropdown" KEY.options="KEY_OPTIONS"
  // export function infraredValue(parameter: any) {
  //   let key = parameter.KEY.code;
  //   Generator.addCode(`String("${key}")`);
  // }

  //% block="init the RGB light" blockType="command"
  export function initLed() {
    Generator.addInclude("DFRobot_NeoPixel.h", "#include <DFRobot_NeoPixel.h>");
    Generator.addObject(`neoPixel_17`, `DFRobot_NeoPixel`, `neoPixel_17;`);
    Generator.addSetup(`neoPixel_17.begin`, `neoPixel_17.begin(17, 6);`);
  }

  //% block="RGB light Set the brightness of the pin light to [SR]" blockType="command"
  //% SR.shadow="range"   SR.params.min=0    SR.params.max=255    SR.defl=255
  export function setLight(parameter: any, block: any) {
    let light = parameter.SR.code;
    Generator.addCode(`neoPixel_17.setBrightness(${light});`);
  }

  //% block="RGB light pin [NUM] display [COLOR]" blockType="command"
  //% NUM.shadow="dropdown" NUM.options="NUM_OPTIONS"
  //% COLOR.shadow="colorPalette"
  export function setColor(parameter: any, block: any) {
    let num = parameter.NUM.code;
    let color = parameter.COLOR.code;

    if (num === "All") {
      Generator.addCode(`neoPixel_17.setRangeColor(${0}, ${5}, ${color});`);
    } else {
      Generator.addCode(`neoPixel_17.setRangeColor(${num}, ${num}, ${color});`);
    }
  }

  //% block="RGB light number from [PIN1] to [PIN2] to display [COLOR]" blockType="command"
  //% PIN1.shadow="range"   PIN1.params.min=1    PIN1.params.max=6    PIN1.defl=1
  //% PIN2.shadow="range"   PIN2.params.min=1    PIN2.params.max=6    PIN2.defl=6
  //% COLOR.shadow="colorPalette"
  export function setRGBLight(parameter: any) {
    let pin1 = parameter.PIN1.code;
    let pin2 = parameter.PIN2.code;
    let color = parameter.COLOR.code;

    Generator.addInclude(`DFRobot_NeoPixel.h`, `#include <DFRobot_NeoPixel.h>`);
    Generator.addObject(`neoPixel_17`, `DFRobot_NeoPixel`, `neoPixel_17;`);
    Generator.addSetup(`neoPixel_17.begin`, `neoPixel_17.begin(17, 6, 255);`);
    Generator.addCode(
      `neoPixel_17.setRangeColor((String(${pin1}).toInt() - 1), (String(${pin2}).toInt() - 1), ${color});`
    );
    // Generator.addCode(`neoPixel_17.setRangeColor(${pin1 - 1}, ${pin2 - 1}, ${color});`);
  }

  //% block="All RGB lamp pin are off" blockType="command"
  export function clearAll(parameter: any, block: any) {
    Generator.addCode(`neoPixel_17.clear();`);
  }

  //% block="Key module [STATE]" blockType="boolean"
  //% STATE.shadow="dropdown"  STATE.options="STATE"
  export function keyModule(parameter: any) {
    let state = parameter.STATE.code;
    Generator.addSetup(`pinMode`, `pinMode(19, INPUT);`);
    Generator.addCode([
      `digitalRead(19) == ${state}`,
      Generator.ORDER_UNARY_POSTFIX,
    ]);
  }

  //% block="Buzzer [STATUS]" blockType="command"
  //% STATUS.shadow="dropdown" STATUS.options="STATUS_OPTIONS"
  export function buzzerStatus(parameter: any) {
    let status = parameter.STATUS.code;
    Generator.addSetup(`pinModeOutput`, `pinMode(18, OUTPUT);`);
    Generator.addCode(`analogWrite(18, ${status});`);
  }

  //% block="set pwm output [SR]" blockType="command"
  //% SR.shadow="range"   SR.params.min=0    SR.params.max=255    SR.defl=255
  export function setPwm(parameter: any) {
    let sr = parameter.SR.code;
    Generator.addCode(`analogWrite(18, ${sr});`);
  }

  //% block="Line patrol [DIRECTION]" blockType="boolean"
  //% DIRECTION.shadow="dropdown" DIRECTION.options="DIRECTION_OPTIONS"
  export function setDirection(parameter: any) {
    let direction = parameter.DIRECTION.code;
    Generator.addSetup(
      `pinModeInput${direction}`,
      `pinMode(${direction}, INPUT);`
    );
    Generator.addCode(`digitalRead(${direction})`);
  }

  //% block="Line patrol [DIRECTION] hit black line" blockType="boolean"
  //% DIRECTION.shadow="dropdown" DIRECTION.options="DIRECTION_OPTIONS"
  export function hitBlackLine(parameter: any) {
    let direction = parameter.DIRECTION.code;
    Generator.addSetup(
      `pinModeInput${direction}`,
      `pinMode(${direction}, INPUT);`
    );
    Generator.addCode([
      `digitalRead(${direction}) == 1`,
      Generator.ORDER_UNARY_POSTFIX,
    ]);
  }

  //% block="Left motor [BEARING1] [SPEED1] right motor [BEARING2] [SPEED2]" blockType="command"
  //% BEARING1.shadow="dropdown" BEARING1.options="BEARING_OPTIONS"
  //% BEARING2.shadow="dropdown" BEARING2.options="BEARING_OPTIONS"
  //% SPEED1.shadow="range"   SPEED1.params.min=0    SPEED1.params.max=255    SPEED1.defl=255
  //% SPEED2.shadow="range"   SPEED2.params.min=0    SPEED2.params.max=255    SPEED2.defl=255
  export function setMotor(parameter: any) {
    let bearing1 = parameter.BEARING1.code;
    let bearing2 = parameter.BEARING2.code;
    let speed1 = parameter.SPEED1.code;
    let speed2 = parameter.SPEED2.code;

    let leftOne = 0,
      leftTwo = 0,
      rightOne = 0,
      rightTwo = 0;

    if (bearing1 === "forword") {
      leftOne = 1;
      leftTwo = 0;
    } else if (bearing1 === "back") {
      leftOne = 0;
      leftTwo = 1;
    }

    if (bearing2 === "forword") {
      rightOne = 1;
      rightTwo = 0;
    } else if (bearing2 === "back") {
      rightOne = 0;
      rightTwo = 1;
    }

    Generator.addSetup(`pinMode11`, `pinMode(${11}, OUTPUT);`);
    Generator.addSetup(`pinMode12`, `pinMode(${12}, OUTPUT);`);
    Generator.addSetup(`pinMode14`, `pinMode(${14}, OUTPUT);`);
    Generator.addSetup(`pinMode13`, `pinMode(${13}, OUTPUT);`);

    Generator.addCode(`analogWrite(10, ${speed1});`);
    Generator.addCode(`digitalWrite(11, ${leftOne});`);
    Generator.addCode(`digitalWrite(12, ${leftTwo});`);
    Generator.addCode(`analogWrite(15, ${speed2});`);
    Generator.addCode(`digitalWrite(14, ${rightOne});`);
    Generator.addCode(`digitalWrite(13, ${rightTwo});`);
  }

  //% block="Motor stop" blockType="command"
  export function stopMotor(parameter: any) {
    Generator.addCode(`digitalWrite(11, ${1});`);
    Generator.addCode(`digitalWrite(12, ${1});`);
    Generator.addCode(`digitalWrite(14, ${1});`);
    Generator.addCode(`digitalWrite(13, ${1});`);
  }

  //% block="Set the [PIN] to [SPEED] [DIRECTION]"
  //% PIN.shadow="dropdown" PIN.options="SERVOMOTOR_PIN_OPTIONS"
  //% SPEED.shadow="range"   SPEED.params.min=0    SPEED.params.max=100    SPEED.defl=50
  //% DIRECTION.shadow="dropdown" DIRECTION.options="SERVOMOTOR_DIRECTION_OPTIONS"
  // export function setServoMotor(parameter: any) {
  //   let pin = parameter.PIN.code;
  //   let speed = parameter.SPEED.code;
  //   let direction = parameter.DIRECTION.code;

  //   Generator.addInclude(`DFRobot_Libraries.h`, `#include <DFRobot_Libraries.h>`)
  //   Generator.addObject(`servo360_${pin}`, `DFRobot_Servo360`, `servo360_${pin};`);
  //   Generator.addSetup(`servo360_${pin}`, `servo360_${pin}.attach(${pin});`);
  //   Generator.addCode(`servo360_${pin}.speed(${direction == '1' ? speed : -speed});`);
  // }

  //% block="Set the [PIN] of the servo motor to [DEGREES]" blockType="command"
  //% PIN.shadow="dropdown" PIN.options="SERVOMOTOR_PIN_OPTIONS"
  //% DEGREES.shadow="angle"
  export function setServoMotor(parameter: any) {
    let pin = parameter.PIN.code;
    let degrees = parameter.DEGREES.code;

    Generator.addInclude(`DFRobot_Servo.h`, `#include <DFRobot_Servo.h>`);
    Generator.addObject(`Servo_${pin}`, `Servo`, `servo_${pin};`);
    Generator.addSetup(`servo_${pin}`, `servo_${pin}.attach(${pin});`);
    Generator.addCode(`servo_${pin}.angle(abs(${degrees}));`);
  }

  //% block="digital [PIN] output [LED_STATE]" blockType="command"
  //% PIN.shadow="dropdown" PIN.options="SERVOMOTOR_PIN_OPTIONS"
  //% LED_STATE.shadow="dropdown"  LED_STATE.options="LED_STATE"
  export function setLedState(parameter: any) {
    let pin = parameter.PIN.code;
    let ledState = parameter.LED_STATE.code;

    Generator.addSetup(`pinMode_${pin}_output`, `pinMode(${pin}, OUTPUT);`);
    Generator.addCode(`digitalWrite(${pin}, ${ledState});`);
  }

  //% block="Read the value of the [KNOB] number"  blockType="reporter"
  //% KNOB.shadow="dropdown" KNOB.options="KNOB_OPTIONS"
  export function readKnob(parameter: any) {
    let knob = parameter.KNOB.code;
    Generator.addCode([`analogRead(${knob})`, Generator.ORDER_UNARY_POSTFIX]);
  }

  //% block="Language module startup" blockType="command"
  export function startLanguageModule(parameter: any) {
    Generator.addInclude(
      "volatile_float_ywtyuyin",
      `volatile float ywt_yuyin;`
    );
    Generator.addSetup(`Serial2.begin`, `Serial2.begin(9600);`);
    Generator.addCode(
      `if ((Serial2.available())) {\n` +
        `    ywt_yuyin = Serial2.read();\n` +
        `  }`
    );
  }

  //% block="Language module [VALUE]"  blockType="boolean"
  //% VALUE.shadow="dropdown" VALUE.options="VALUE_OPTIONS"
  export function isLanguageModuleValue(parameter: any) {
    let value = parameter.VALUE.code;
    Generator.addCode(`(ywt_yuyin == ${value})`);
  }

  //% block="The flame sensor [PIN] detects danger"  blockType="boolean"
  //% PIN.shadow="dropdown" PIN.options="SERVOMOTOR_PIN_OPTIONS"
  export function detectDanger(parameter: any) {
    let pin = parameter.PIN.code;

    Generator.addSetup(`pinMode_${pin}_input`, `pinMode(${pin}, INPUT);`);
    Generator.addCode(`(digitalRead(${pin}) == 0)`);
  }

  //% block="Read temperature and humidity [PIN] [TYPE]"  blockType="reporter"
  //% PIN.shadow="dropdown" PIN.options="SERVOMOTOR_PIN_OPTIONS"
  //% TYPE.shadow="dropdown" TYPE.options="TEMPERATURE_HUMIDITY_TYPE"
  export function readTemperatureAndHumidity(parameter: any) {
    let pin = parameter.PIN.code;
    let type = parameter.TYPE.code;

    Generator.addInclude(`DFRobot_DHT.h`, `#include <DFRobot_DHT.h>`);
    Generator.addObject(`dht11_${pin}`, `DFRobot_DHT`, `dht11_${pin};`);
    Generator.addSetup(
      `dht11_${pin}.begin`,
      `dht11_${pin}.begin(${pin}, DHT11);`
    );
    Generator.addCode([
      `dht11_${pin}.${type}()`,
      Generator.ORDER_UNARY_POSTFIX,
    ]);
  }

  //% block="Read the value of the [KNOB] number"  blockType="reporter"
  //% KNOB.shadow="dropdown" KNOB.options="KNOB_OPTIONS"
  export function readPhotosensitivity(parameter: any) {
    let knob = parameter.KNOB.code;
    Generator.addCode([`analogRead(${knob})`, Generator.ORDER_UNARY_POSTFIX]);
  }

  //% block="Initialize the color sensor" blockType="command"
  export function initializeColorSensor(parameter: any) {
    Generator.addInclude(
      "initializeColorSensor",
      `unsigned char Re_buf[11],ywt_ideal_counter=0;\n` +
        `unsigned char ywt_ideal_sign=0;\n` +
        `int ywt_ideal_red = 0;\n` +
        `int ywt_ideal_green = 0;\n` +
        `int ywt_ideal_blue = 0;\n` +
        `void ywt_ideal_serialEvent();\n` +
        `\n` +
        `void ywt_ideal_serialEvent() {\n` +
        `  while (Serial2.available()) {\n` +
        `    Re_buf[ywt_ideal_counter]=(unsigned char)Serial2.read();\n` +
        `    if(ywt_ideal_counter==0&&Re_buf[0]!=0x5A) return;\n` +
        `    ywt_ideal_counter++;\n` +
        `    if(ywt_ideal_counter==8) {\n` +
        `      ywt_ideal_counter=0;\n` +
        `      ywt_ideal_sign=1;\n` +
        `    }\n` +
        `  }\n` +
        `}`
    );

    Generator.addSetup(`Serial2.begin`, `Serial2.begin(9600);`);

    Generator.addCode(
      `unsigned char ywt_ideal_i=0,ywt_ideal_sum=0;\n` +
        `  ywt_ideal_serialEvent();\n` +
        `  if(ywt_ideal_sign) {\n` +
        `     ywt_ideal_sign=0;\n` +
        `     for(ywt_ideal_i=0;ywt_ideal_i<7;ywt_ideal_i++)\n` +
        `      ywt_ideal_sum+=Re_buf[ywt_ideal_i];\n` +
        `     if(ywt_ideal_sum==Re_buf[ywt_ideal_i] ) {\n` +
        `         ywt_ideal_red=Re_buf[4];\n` +
        `         ywt_ideal_green=Re_buf[5];\n` +
        `         ywt_ideal_blue=Re_buf[6];\n` +
        `     }\n` +
        `   }\n`
    );
  }

  //% block="The color sensor recognizes the value of [COLOR]" blockType="reporter"
  //% COLOR.shadow="dropdown" COLOR.options="COLOR_SENSOR_COLOR_OPTIONS"
  export function colorSensor(parameter: any) {
    let color = parameter.COLOR.code;
    Generator.addCode(color);
  }

  //% block="The raindrop module [PIN] detects rain"  blockType="boolean"
  //% PIN.shadow="dropdown" PIN.options="SERVOMOTOR_PIN_OPTIONS"
  export function detectRain(parameter: any) {
    let pin = parameter.PIN.code;

    Generator.addSetup(`pinMode_${pin}_input`, `pinMode(${pin}, INPUT);`);
    Generator.addCode(`(digitalRead(${pin}) == 0)`);
  }
}
